//app.js
var api = require('utils/api.js');
var tool = require('utils/tool.js');
import '/plugins/weapp-cookie/weapp-cookie'
App({
  onLaunch: function () {
    // 展示本地存储能力
    // var logs = wx.getStorageSync('logs') || []
    // logs.unshift(Date.now())
    // wx.setStorageSync('logs', logs)
    //登录
    var swUser = wx.getStorageSync('swUser')
    if (swUser) {
      this.globalData.swUser = swUser;
      this.globalData.userInfo = swUser;
      this.globalData.hasUserInfo = true;
      this.updateVIP();
    }
    // wx.checkSession({
    //   success: function () {

    //   },
    //   error: function () {
    //     this.login();
    //   }
    // })
    // 获取用户信息
    // wx.getSetting({
    //   success: res => {
    //     if (res.authSetting['scope.userInfo']) {
    //       // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
    //       wx.getUserInfo({
    //         success: res => {
    //           // 可以将 res 发送给后台解码出 unionId
    //           this.globalData.userInfo = res.userInfo

    //           // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
    //           // 所以此处加入 callback 以防止这种情况
    //           if (this.userInfoReadyCallback) {
    //             this.userInfoReadyCallback(res)
    //           }
    //         }
    //       })
    //     }
    //   }
    // })
  },

  updateUser: function (swUser) {
    this.globalData.swUser = swUser;
    this.globalData.userInfo = swUser;
    this.globalData.hasUserInfo = true;
    wx.setStorageSync('swUser', swUser);
  },
  updateVIP: function(_this) {
    // var date = (new tool.MyDate()).Format('yyyy-MM-dd');
    // if (!this.globalData.swUser || !this.globalData.swUser.vip_end_date || date >= this.globalData.swUser.vip_end_date) {
    //   this.globalData.vipEndDays = 0;
    // } else {
    //   this.globalData.vipEndDays = this.diffDays(this.globalData.swUser.vip_end_date, date);
    // }
    if(!this.globalData.userInfo) {
      return false
    }
    var that = this;
    wx.requestWithCookie({
      url: api.getVIPRemainDaysUrl,
      method: 'GET',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      data: {},
      dataType: 'json',
      success: function (res) {
        if (res.data.Code === 1) {
          that.globalData.vipEndDays = res.data.Entity;
          if (typeof (_this) != "undefined") {
            _this.setData({
              vipEndDays: res.data.Entity
            });
          }
        } else {
          
        }
      },
      fail: function () {
        
      }
    })
  },
  diffDays(date1, date2) {
    var dateSpan, tempDate, iDays;
    date1 = Date.parse(date1);
    date2 = Date.parse(date2);
    dateSpan = date2 - date1;
    dateSpan = Math.abs(dateSpan);
    iDays = Math.floor(dateSpan / (24 * 3600 * 1000));
    return iDays;
  },
  startDevice(dno) {
    if (!dno) {
      wx.showToast({
        icon: 'none',
        title: '请扫码后启动设备',
      })
      return false
    }

    wx.showLoading({
      title: '设备启动中',
    })
    console.log(api.startDeviceUrl);
    console.log({
      DNO: dno
    })
    wx.request({
      url: api.startDeviceUrl,
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      data: {
        DNO: dno
      },
      dataType: 'json',
      success: function (res) {
        console.log(res)
        wx.hideLoading()
        if (res.data.Code === 1) {
          wx.showToast({
            title: '启动成功',
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: res.data.Msg,
          })
        }
      },
      fail: function (res) {
        wx.hideLoading()
        wx.showToast({
          title: '启动失败',
        })
      },
      complete: function (res) {
        wx.setStorageSync('start-dno', null)
      }
    })
  },
  globalData: {
    userInfo: null,
    swUser: null,
    hasUserInfo: false,
    bindPhone: false,
    vipEndDays: 0,
    dno: null,
    appId: 'wxc6030201c9c00082'
  }
})